package com.demo.springcloud.hystrix;

import com.netflix.hystrix.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

/**
 * 自定义Hystrix请求
 */
public class MyHystrixCommand extends HystrixCommand<String> {


    private RestTemplate restTemplate;

    public MyHystrixCommand(Setter setter,RestTemplate restTemplate){
        super(setter);
        this.restTemplate = restTemplate;

    }
    @Override
    protected String run() throws Exception {
        return restTemplate.getForEntity("http://01-springcloud-service-provider/service/hello",String.class).getBody();
    }


    @Override   // 当服务降级/熔断以后会执行此方法
    protected String getFallback() {
        // 实现服务降级熔断逻辑
      Throwable throwable =   super.getExecutionException();
        System.out.println(throwable.getMessage());
        return "error";
    }
}
